
<style>
    .xxpayPopupDiv .layui-form-label {
        width:15% !important;
        margin-left:1%;
    }
    .xxpayPopupDiv .layui-input-inline {
        width: 43% !important;
    }
</style>

<div class="layui-fluid">

    <div class="layui-card">

        <div class="layui-form layui-form-pane">

            <div class="layui-tab layui-tab-brief">

                <ul class="layui-tab-title"><li class="layui-this">设置</li></ul>

                <div class="layui-tab-content">

                    <div class="layui-form-item">
                        <label class="layui-form-label">设置用户：</label>
                        <div class="layui-input-inline">
                            <input readonly type="text" id="setUserInfo"  class="layui-input layui-bg-gray">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">重置用户超级密码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="supPassword" placeholder="（若不更改请置空）" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">当前用户是否绑定谷歌验证码</label>
                        <div class="layui-input-inline" style="padding: 10px;">

                            <input hidden name="unBindGoogleCodeFlag" id="unBindGoogleCodeFlag" value="0" />
                            <div class="layui-hide showUserGoogleCodeUnBindDiv">
                                <span class="layui-badge">用户未绑定</span>
                            </div>
                            <div class="layui-hide showUserGoogleCodeBindDiv">
                                <span style="color: green">用户已绑定</span>
                                <button class="layui-btn layui-btn-xs layui-btn-primary layui-btn-inline setUserUnBindBtn">解除绑定</button>
                            </div>
                            <div class="layui-hide showSetUserGoogleCodeUnBindDiv">
                                <span style="color: red">更新为【未绑定】</span>
                                <button class="layui-btn layui-btn-xs layui-btn-primary layui-btn-inline setUserCancelUnBindBtn">取消该操作</button>
                            </div>

                        </div>

                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">敏感操作时的验证方式</label>
                        <div class="layui-input-block">
                            <input type="radio" name="securityCheckType" value="2" title="谷歌验证码(推荐)">
                            <input type="radio" name="securityCheckType" value="1" title="超级密码">
                            <input type="radio" name="securityCheckType" value="3" title="谷歌验证+超级密码">
                            <input type="radio" name="securityCheckType" value="0" title="无验证">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">变更IP是否需要重新登录</label>
                        <div class="layui-input-block">
                            <input type="radio" name="ipChangeNeedReLogin" value="1" title="重新登录">
                            <input type="radio" name="ipChangeNeedReLogin" value="0" title="无需重新登录（不建议）">
                        </div>
                    </div>

                    <button type="button" class="layui-hide xxpayYesBtn" lay-submit="" lay-filter="securitySetFilter">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>




<script>
    layui.use('form',function(){
        var form = layui.form
            ,$ = layui.$
            ,element = layui.element
            ,layer = layui.layer
            ,view = layui.view
            ,admin = layui.admin;
        // 导航
        element.render('breadcrumb', 'breadcrumb');
        var userId = view.getOpenParams('userId');
        var userInfo = {};
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/sys/user/get',
            data: {userId : userId},
            error: function(err){layer.alert(JSON.stringify(err.field), {title: '错误提示'})},
            success: function(res){
                if(res.code == 0){

                    $('#setUserInfo').val(res.data.userName + "  (" + res.data.userId + ")");
                    $("input[name='securityCheckType'][value='"+res.data.securityCheckType+"']").attr("checked", true);
                    $("input[name='ipChangeNeedReLogin'][value='"+res.data.ipChangeNeedReLogin+"']").attr("checked", true);

                    if(res.data.googleAuthStatus == 1){ //用户已绑定谷歌验证码
                        $(".showUserGoogleCodeBindDiv").removeClass("layui-hide");
                    }else{
                        $(".showUserGoogleCodeUnBindDiv").removeClass("layui-hide");
                    }

                    userInfo = res.data;
                    form.render();
                }
            }
        });

        /* 自定义验证规则 */
        form.verify({
            pass: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(value != "" && (value.length < 6 || value.length > 12)){
                    return '密码必须6到12位';
                }
            }
        });

        form.on('submit(securitySetFilter)', function(data) {

            data.field.userId = userId;


            if(data.field.securityCheckType == 2){  //谷歌验证码


                if(userInfo.googleAuthStatus != '1' || data.field.unBindGoogleCodeFlag == 1 ){ //判断用户是否绑定了谷歌验证码
                    return layer.msg("当前用户未绑定谷歌验证， 请先绑定谷歌验证！");
                }

            }else if(data.field.securityCheckType == 1){ //超级密码方式

                if( !userInfo.supPassword && !data.field.supPassword ){ //判断用户是否设置了超级密码
                    return layer.msg("用户未设置超级密码， 请先设置超级密码！");
                }

            }else if(data.field.securityCheckType == 3){ //谷歌验证 + 超级超级密码

                if(userInfo.googleAuthStatus != '1'  || data.field.unBindGoogleCodeFlag == 1 ){ //判断用户是否绑定了谷歌验证码
                    return layer.msg("当前用户未绑定谷歌验证， 请先绑定谷歌验证！");
                }

                if( !userInfo.supPassword && !data.field.supPassword ){ //判断用户是否设置了超级密码
                    return layer.msg("用户未设置超级密码， 请先设置超级密码！");
                }
            }

            admin.req({
                type: "POST",
                url: layui.setter.baseUrl + '/sys/user/security_set',
                data: data.field,
                success: function(res){
                    if(res.code == 0) {
                        layer.alert(res.msg, {title: "保存成功"}, function(index){
                            layer.closeAll(); //关闭所有弹层
                            layui.table.reload('tableReload'); //调用业务弹层外表格重新加载
                        });
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //点击【解除绑定】
        $(".setUserUnBindBtn").click(function(){
            $(".showUserGoogleCodeBindDiv").addClass("layui-hide");
            $(".showSetUserGoogleCodeUnBindDiv").removeClass("layui-hide");
            $("#unBindGoogleCodeFlag").val("1");
        });

        //点击【取消该操作】
        $(".setUserCancelUnBindBtn").click(function(){
            $(".showUserGoogleCodeBindDiv").removeClass("layui-hide");
            $(".showSetUserGoogleCodeUnBindDiv").addClass("layui-hide");
            $("#unBindGoogleCodeFlag").val("0");
        });

    })
</script>

